<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ taglib prefix="bigk" uri="/bigk-tags"%>
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>

<s:if test="id == null">
    <s:set var="actionVariant">create</s:set>
    <s:set var="actionName">invoices/save</s:set>
    <s:set var="titleKey">invoicesaction.title.create</s:set>
</s:if>
<s:else>
    <s:set var="actionVariant">update</s:set>
    <s:set var="actionName">invoices/update</s:set>
    <s:set var="titleKey">invoicesaction.title.update</s:set>
</s:else>

<html>

  <head>
    <title><s:text name="invoicesaction.title"/></title>
  </head>

  <body>

    <p class="wwPageTitle">
      <img src="<s:url value='/images/invoice.gif'/>"/>
      <s:text name="%{#titleKey}"/>
    </p>

    <s:actionerror />
    <s:actionmessage/>
    <s:fielderror />

    <s:form id="invoiceInputForm"
            action="%{#actionName}"
            method="POST"
            cssClass="wwFilterTable" >

      <s:hidden name="id" />

      <tr>
        <th colspan="2">
          <s:text name="%{#titleKey}"/>
        </th>
      </tr>

      <s:label key="invoice.id"
               name="model.id"
               value="%{model.id}" />

      <s:textfield key="invoice.year"
                   name="model.year"
                   required="true"
                   size="5"/>

      <s:textfield key="invoice.number"
                   name="model.number"
                   required="true"
                   size="5"/>

      <tr>
        <th colspan="2">
          <s:text name="invoice.purchaserData"/>
        </th>
      </tr>

      <tr>
        <td colspan="2">
          <SCRIPT type="text/javascript">
          function showPurchasersChooser() {
              var url = '<s:url namespace="/choosers" action="purchasers/list" />';
        	  window.open(url, 'PurchasersChooser', 'width=580,height=750,top=100,left=200,resizable=yes,scrollbars=yes');
          }
          function purchasersChooserChoosed(name, address, nip) {
        	  var obj = null;
        	  obj = document.getElementById("purchaserNameId");
        	  if (obj) obj.value = name;

              var obj = null;
              obj = document.getElementById("purchaserAddressId");
              if (obj) obj.value = address;

              var obj = null;
              obj = document.getElementById("purchaserNipId");
              if (obj) obj.value = nip;
          }
          </SCRIPT>

          <s:url id="imageLink" value='/images/purchaser.gif'/>          
          <p class="infoText">
			  <img src="<s:url value='/images/info.gif'/>"/>
	          <s:text name="invoicesaction.purchaserData.prompt">
	            <s:param value="%{'showPurchasersChooser()'}" />
	            <s:param value="%{#imageLink}" />
	          </s:text>          
          </p>
        </td>
      </tr>

      <s:textarea id="purchaserNameId"
                  key="invoice.purchaserName"
                  name="model.purchaserName"
                  required="true"
                  cols="75"
                  rows="2" />
               
      <s:textarea id="purchaserAddressId"
                  key="invoice.purchaserAddress"
                  name="model.purchaserAddress"
                  required="true"
                  cols="75"
                  rows="2" />

      <s:textfield id="purchaserNipId"
                   key="invoice.purchaserNip"
                   name="model.purchaserNip"
                   size="15"
                   maxlength="13" />

      <tr>
        <th colspan="2">
          <s:text name="invoice.timeData"/>
        </th>
      </tr>

      <sx:datetimepicker key="invoice.documentDate"
                         name="model.documentDate"
                         displayFormat="dd-MM-yyyy"
                         required="true" />

      <sx:datetimepicker key="invoice.soldDate"
                         name="model.soldDate"
                         displayFormat="dd-MM-yyyy"
                         required="true"
                         valueNotifyTopics="/soldDateTopic" />

      <script type="text/javascript">
        dojo.event.topic.subscribe("/soldDateTopic", function(textEntered, date, widget){
        	dojo.event.topic.publish("/paymentDateDivReload");
        });
      </script>

      <tr>
        <td class="tdLabel">
          <s:label key="invoice.paymentDate"
                   theme="simple"
                   cssClass="label" />:
        </td>
        <td>
        
          <s:url id="paymentDateDivUrl" namespace="/nodecorate" action="PaymentDateAction" />
          <sx:div id="paymentDateDiv"
                  href="%{#paymentDateDivUrl}"
                  listenTopics="/paymentDateDivReload"
                  formId="invoiceInputForm"
                  preload="true"
                  showLoadingText="true"
                  loadingText="didi ma przeladowanie..."
                  theme="ajax" >

	          <s:text id="paymentDate" name="format.date">
	            <s:param value="model.paymentDate"/>
	          </s:text>
	          <s:property value="%{#paymentDate}" />
	          
              <s:hidden name="model.paymentDate" />
              
          </sx:div>
          
        </td>
      </tr>

      <s:select key="invoice.paymentKind"
                name="model.paymentKindId"
                required="true"
                list="paymentKindsList"
                listKey="id"
                listValue="description"
                onchange='dojo.event.topic.publish("/paymentDateDivReload")' />

      <bigk:form-table-close />
      <br/>
      
      <div class="wwPageMenu">
	    <s:url id="url" action="invoices/createInvoicePosition" />
	    <a href="#"
	       class="wwDataTable"
	       onclick="document.forms[0].action='<s:property value="#url"/>'; document.forms[0].submit();">
	      <img src="<s:url value='/images/add.gif'/>"/>
	      <s:text name="invoicesaction.addInvoicePosition"/>
	    </a>
	  </div>
      
      <bigk:form-table-open cssClass="wwFilterTable" />

      <tr>
        <th rowspan="2"><s:text name="invoicePosition.quantity"/></th>
        <th rowspan="2"><s:text name="invoicePosition.serviceName"/></th>
        <th rowspan="2"><s:text name="invoicePosition.symbol"/></th>
        <th rowspan="2"><s:text name="invoicePosition.measure"/></th>
        <th rowspan="2"><s:text name="invoicePosition.priceNetto"/></th>
        <th rowspan="2" class="colValueNetto"><s:text name="invoicePosition.valueNetto"/></th>
        <th colspan="2"><s:text name="invoicePosition.tax"/></th>
        <th rowspan="2" class="colTotal"><s:text name="invoicePosition.total"/></th>
        <th rowspan="2" class="widthPercent1 nowrap">&nbsp;</th>
      </tr>

      <tr>
        <th class="colInterestRate"><s:text name="invoicePosition.interestRate"/></th>
        <th class="colSum"><s:text name="invoicePosition.sum"/></th>
      </tr>
      
      <s:iterator value="model.invoicePositions" status="status">
      
      <tr>
        <td class="integer">
          <s:property value="quantity"/>
        </td>
        <td>
          <s:property value="serviceName"/>
        </td>
        <td>
          <s:property value="symbol"/>
        </td>
        <td>
          <s:property value="measure"/>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="priceNetto"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="valueNetto"/>
          </s:text>
        </td>
        <td class="integer">
          <s:text name="format.integer">
            <s:param value="interestRate"/>
          </s:text>%
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="sum"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="total"/>
          </s:text>
        </td>
        <td class="widthPercent1 nowrap">
          <s:url action="invoices/deleteInvoicePosition" id="url" includeParams="none">
            <s:param name="pId" value="#status.index"/>
          </s:url>
          <s:a href="%{url}">
            <img src="<s:url value='/images/delete.gif'/>"/>
          </s:a>
        </td>
      </tr>
      
      </s:iterator>

      <%-- separator --%>
      <tr>
        <td colspan="10" class="cellUnderlined">&nbsp;</td>
      </tr>
      
      <%-- podsumowanie --%>
      <tr>
        <td colspan="4">&nbsp;</td>
        <td class="money">
          <b><s:text name="invoicePosition.allTogether"/></b>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[1.0].valueNetto"/>
          </s:text>
        </td>
        <td>
          &nbsp;
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[1.0].sum"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[1.0].total"/>
          </s:text>
        </td>
        <td class="widthPercent1 nowrap">
          &nbsp;
        </td>
      </tr>

    <s:iterator id="key" value="model.calculatedSummariesKeys" status="status">
      <tr>
        <td colspan="4">&nbsp;</td>
        <td class="money">
          <s:if test="#status.first">
            <b><s:text name="invoicePosition.allTogetherFactors"/></b>
          </s:if>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[#key].valueNetto"/>
          </s:text>
        </td>
        <td class="integer">
          <s:text name="format.integer">
            <s:param value="model.calculatedSummaries[#key].interestRate"/>
          </s:text>%
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[#key].sum"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[#key].total"/>
          </s:text>
        </td>
        <td class="widthPercent1 nowrap">
          &nbsp;
          
        </td>
      </tr>
      </s:iterator>
      
      <bigk:form-table-break cssClass="wwFilterTable" />

      <tr>
        <td colspan="2" class="wwFilterTableButtonBar">
          <s:submit key="button.submit" cssClass="inputButton" theme="simple"/>
          <s:submit key="button.cancel" cssClass="inputButton" theme="simple" action="invoices/list" />
        </td>
      </tr>
      
    </s:form>

  </body>

</html>
